import app from "./app";

import {shiQinGetFamilyId,shiQinGetFamilyDetail} from "../../api/shiqinAPI.js"
let state = {
    selectedId:uni.getStorageSync('selectedFamilyId') || '',
    selectedFamilyDetail:uni.getStorageSync('selectedFamilyDetail') || {}
}
let mutations = {
    setSelectedFamily(state, data) {
        uni.setStorageSync('selectedFamilyId', data.id);
        state.selectedId = data.id;
    },
    setSelectedFamilyDetail(context, data) {
        uni.setStorageSync('selectedFamilyDetail', data.detail);
        state.selectedFamilyDetail = data.detail;
    },
    // 点赞家庭圈
    setFamilyCircleLike(state, data){
        state.selectedFamilyDetail.family_circle[data.index].is_click = data.is_click;
        state.selectedFamilyDetail.family_circle[data.index].click_num = data.click_num;
        let index = state.selectedFamilyDetail.family_circle[data.index].record.indexOf(data.username);
        if(index == -1){
            state.selectedFamilyDetail.family_circle[data.index].record.push(data.username);
        }else{
            state.selectedFamilyDetail.family_circle[data.index].record.splice(index,1);
        }
    },
    // 添加家庭圈列表
    addFamilyCircleList(state, data){
        let arr = []
        data.forEach(item=>{
            arr.push(item)
        })
        state.selectedFamilyDetail.family_circle = state.selectedFamilyDetail.family_circle.concat(arr)
    }
}
let actions = {
    setSelectedFamilyActions(context) {
        shiQinGetFamilyId().then(res=>{
            context.commit('setSelectedFamily', res.data)
            context.dispatch('setFamilyDetail',res)
        })
    },
    setFamilyDetail(context){
        shiQinGetFamilyDetail({family_id:context.state.selectedId}).then(info =>{
            context.commit('setSelectedFamilyDetail', info.data)
        })
    }
}

export default {
    state,
    mutations,
    actions
}